<?php

/**
 * 基础库管理
 *各类下拉框设置 、暂且是这样
 * Created by PhpStorm.
 * User: Apache
 * Date: 2019/10/27
 * Time: 10:02
 */

namespace app\api\controller\basemanage;

use app\api\controller\common\ApiBase;
use app\api\controller\common\CommonPlus;

class Companyset extends ApiBase
{

    protected $admin;
    protected $model = null;
    public function _initialize()
    {
        $this->admin = session('admin');
        $this->model = model('Companyset');
    }

    /**
     * 企业设置列表
     */
    public function companysetList()
    {
        $keyword = $this->request->param('keyword');
        $page = $this->request->param('page');
        $where = array();
        $page = max(1, intval($page));
        $pagesize = 10;

        $where['is_delete'] = 0; //回头问下

        if (!empty($keyword)) {
            $where['company_name|company_name_en'] = array('like', '%' . $keyword . '%');
        }

        $list = $this->model->listWherePage($where, 'id desc', ($page - 1) * $pagesize, $pagesize);
        $total = $this->model->countWhere($where);

        return json(['code' => 1, 'msg' => 'success', 'data' => ['list' => $list, 'total' => $total, 'countpage' => ceil($total / $pagesize)]]);
    }

    /**
     * 详细
     * @return \think\response\Json|void
     */
    public function companysetDetail()
    {
        $id = $this->request->param('id');

        // $where['id'] = $id;
        $where['is_delete'] = 0;
        $info = $this->model->findWhere($where);

        return json(['code' => 1, 'msg' => 'success', 'data' => $info]);
    }

    /**
     * 新增企业设置
     * @return \think\response\Json|void
     */
    public function addCompanyset()
    {
        //==common
        $create_uid  = $this->admin['id'];
        //===common
        //$remark = $this->request->param('remark'); 
        $id = $this->request->param('id');
        $content = $this->request->param('content');  // 自定义属性  


        $where = ['id' => $id, 'is_delete' => 0];
        $r = $this->model->countWhere($where);

        if (empty($r)) {
            //return json(['code'=>0,'msg'=>'记录不存在，请核实']);
            $data = array(
                'create_uid' => $create_uid, 'create_time' => time(),
                'content' => $content
            );
            $r1 = $this->model->insertData($data);
            if (!$r1) {
                return json(['code' => 0, 'msg' => '操作失败，请重试']);
            }
        } else {
            $data = array('update_time' => time(), 'content' => $content);
            $where1['id'] = $id;
            $r = $this->model->updateBywhere($data, $where1);
            if (!$r) {
                return json(['code' => 0, 'msg' => '操作失败，请重试']);
            }
        }



        return json(['code' => 1, 'msg' => '操作成功']);
    }

    public function editCompanyset()
    {
        //common
        $create_uid  = $this->admin['id'];
        //
        $id = $this->request->param('id');
        $company_nature = $this->request->param('company_nature');     //性质

        $remark = $this->request->param('remark');   // yuliu    

        $data = array('company_nature' => $company_nature, 'create_uid' => $create_uid, 'remark' => $remark, 'update_time' => time());
        if (empty($id)) {
            return json(['code' => 0, 'msg' => '参数有误，请核查']);
        }

        $where['id'] = $id;
        $r = $this->model->updateBywhere($data, $where);

        if (!$r) {
            return json(['code' => 0, 'msg' => '操作失败，请重试']);
        }

        return json(['code' => 1, 'msg' => '操作成功']);
    }

    /**
     * 删除项目
     */
    public function delCompanyset()
    {
        $id = $this->request->param('id');
        $create_uid  = $this->admin['id'];

        $where = ['id' => $id, 'create_uid' => $create_uid, 'is_delete' => 0];
        $r = $this->model->countWhere($where);

        if (empty($r)) {
            return json(['code' => 0, 'msg' => '记录不存在，请核实']);
        }

        $r = $this->model->deleteWhere(['id' => $id]);
        if (!$r) {
            return json(['code' => 0, 'msg' => '操作失败，请重试']);
        }

        return json(['code' => 1, 'msg' => '操作成功']);
    }

    //企业形态 这个按道理应该单独分开 基础资料公共区去整  暂时先放着follow 企业




    // ces

    public function test1()
    {
        //==common
        $create_uid  = $this->admin['id'];
        //
        $company_name = 11; //公司中文名

        $company_name_en = 2; //    
        $company_describe = 3; //简介             

        $data = array(
            'create_time' => time(), 'company_name' => $company_name,
            'company_name_en' => $company_name_en, 'company_describe' => $company_describe

        );

        // var_dump ($data);die;
        $r = $this->model->insertData($data);

        if (!$r) {
            return json(['code' => 0, 'msg' => '操作失败，请重试']);
        }

        return json(['code' => 1, 'msg' => '操作成功']);
    }

    //ces end


    /**
     *   // 企业设置填单页面的相关  仅做下拉来源调取 用 ；  相关方法 集合 
     *      暂时先分表，其他未知属性 另行添加  stj
     */
    public function add_xla_Bytype()
    {
        //==common
        $create_uid  = $this->admin['id'];
        $type = $this->request->param('type');     //下拉属性
        //

        if (trim($type) == "gsxz") {
            // 公司性质
            $tbname = "xiala_gsxz"; // gtab
            $title = $this->request->param('title');
            $data = array('create_time' => time(), 'create_uid' => $create_uid, 'title' => $title);
        } elseif (trim($type) == "gsxt") {
            // 公司形态
            $tbname = "xiala_gsxt"; // gtab
            $title = $this->request->param('title');
            $data = array('create_time' => time(), 'create_uid' => $create_uid, 'title' => $title);
        }elseif (trim($type) == "gslevel") {
            // 公司级别
            $tbname = "xiala_gslevel"; 
            $title = $this->request->param('title');
            $data = array('create_time' => time(), 'create_uid' => $create_uid, 'title' => $title);
        }elseif (trim($type) == "sshy") {
            // 所属行业 
            $tbname = "xiala_sshy"; 
            $title = $this->request->param('title');
            $data = array('create_time' => time(), 'create_uid' => $create_uid, 'title' => $title);
        }elseif (trim($type) == "ywxz") {
            // 业务性质 
            $tbname = "xiala_ywxz"; 
            $title = $this->request->param('title');
            $data = array('create_time' => time(), 'create_uid' => $create_uid, 'title' => $title);
        }

        // ----------
        $r = $this->model->inset_common_xla($data, $tbname);
        // var_dump ($r);die;
        if (!$r) {
            return json(['code' => 0, 'msg' => '操作失败，请重试']);
        }

        return json(['code' => 1, 'msg' => '操作成功']);
    }
    // 编辑下拉
    public function edit_xla_Bytype()
    {
        //==common
        $create_uid  = $this->admin['id'];
        $id = $this->request->param('id');
        $type = $this->request->param('type');     //下拉属性
        $status = $this->request->param('status');
        $title = $this->request->param('title');
        $status=0; // 状态显示不走编辑，默认给个0 吧
        //

        if (trim($type) == "gsxz") {
            // 公司性质
            $tbname = "xiala_gsxz"; // gtab        

            $where = array('id' => $id);
            $data = array('update_time' => time(), 'create_uid' => $create_uid, 'title' => $title, 'is_delete' => $status);
        } elseif (trim($type) == "gsxt") {
            // 公司形态
            $tbname = "xiala_gsxt"; // gtab

            $where = array('id' => $id);
            $data = array('update_time' => time(), 'create_uid' => $create_uid, 'title' => $title, 'is_delete' => $status);
        }elseif (trim($type) == "gslevel") {
            // 公司级别
            $tbname = "xiala_gslevel"; 
            $where = array('id' => $id);
            $data = array('update_time' => time(), 'create_uid' => $create_uid, 'title' => $title,'is_delete' => $status);
      
        }elseif (trim($type) == "sshy") {
            // 所属行业 
            $tbname = "xiala_sshy"; 
            $where = array('id' => $id);
            $data = array('update_time' => time(), 'create_uid' => $create_uid, 'title' => $title,'is_delete' => $status);
      
        }elseif (trim($type) == "ywxz") {
            // 业务性质 
            $tbname = "xiala_ywxz"; 
            $where = array('id' => $id);
            $data = array('update_time' => time(), 'create_uid' => $create_uid, 'title' => $title,'is_delete' => $status);
      
        }
        // var

        // ----------
        $r = $this->model->edit_common_xla($data, $where, $tbname);
        // var_dump ($r);die;
        if (!$r) {
            return json(['code' => 0, 'msg' => '操作失败，请重试']);
        }

        return json(['code' => 1, 'msg' => '操作成功']);
    }

    // 删除下拉  弃用
    public function del_xla_Bytype()
    {
        //==common
        $create_uid  = $this->admin['id'];
        $id = $this->request->param('id');
        $type = $this->request->param('type');     //下拉属性

        //

        if (trim($type) == "gsxz") {
            // 公司性质
            $tbname = "xiala_gsxz"; // gtab        
            $is_delete = 1;
            $where = array('id' => $id);
            $data = array('update_time' => time(), 'create_uid' => $create_uid, 'is_delete' => $is_delete);
        } elseif (trim($type) == "gsxt") {
            // 公司形态
            $tbname = "xiala_gsxt"; // gtab
            $is_delete = 1;
            $where = array('id' => $id);
            $data = array('update_time' => time(), 'create_uid' => $create_uid, 'is_delete' => $is_delete);
        }elseif (trim($type) == "gslevel") {
            // 公司级别
            $tbname = "xiala_gslevel"; 
            $is_delete = 1;
            $where = array('id' => $id);
            $data = array('update_time' => time(), 'create_uid' => $create_uid, 'is_delete' => $is_delete);
      
        }elseif (trim($type) == "sshy") {
            // 所属行业 
            $tbname = "xiala_sshy"; 
            $is_delete = 1;
            $where = array('id' => $id);
            $data = array('update_time' => time(), 'create_uid' => $create_uid, 'is_delete' => $is_delete);
      
        }elseif (trim($type) == "ywxz") {
            // 业务性质 
            $tbname = "xiala_ywxz"; 
            $is_delete = 1;
            $where = array('id' => $id);
            $data = array('update_time' => time(), 'create_uid' => $create_uid, 'is_delete' => $is_delete);
      
        }
        // var

        // ----------
        $r = $this->model->edit_common_xla($data, $where, $tbname);
        // var_dump ($r);die;
        if (!$r) {
            return json(['code' => 0, 'msg' => '操作失败，请重试']);
        }

        return json(['code' => 1, 'msg' => '操作成功']);
    }

    // 查询下拉  获取所有列表 按照类别来
    public function selAll_xla_Bytype()
    {
        //==common
        $create_uid  = $this->admin['id'];
        // $id = $this->request->param('id');    
        $type = $this->request->param('type');     //下拉属性

        //

        if (trim($type) == "gsxz") {
            // 公司性质
            $tbname = "xiala_gsxz"; // gtab     

            $where = array('is_delete' => 0);
            // $where = "";
            $data = array('update_time' => time(), 'create_uid' => $create_uid);
        } elseif (trim($type) == "gsxt") {
            // 公司形态
            $tbname = "xiala_gsxt"; // gtab

            $where = array('is_delete' => 0);
            $data = array('update_time' => time(), 'create_uid' => $create_uid);
        }elseif (trim($type) == "gslevel") {
            // 公司级别
            $tbname = "xiala_gslevel"; 
            $where = array('is_delete' => 0);
            $data = array('update_time' => time(), 'create_uid' => $create_uid);
        }elseif (trim($type) == "sshy") {
            // 所属行业 
            $tbname = "xiala_sshy";   
            $where = array('is_delete' => 0);
            $data = array('update_time' => time(), 'create_uid' => $create_uid);
        }elseif (trim($type) == "ywxz") {
            // 业务性质 
            $tbname = "xiala_ywxz"; 
            $where = array('is_delete' => 0);
            $data = array('update_time' => time(), 'create_uid' => $create_uid);
        }
        // var

        // ----------
        $list = $this->model->selAll_common_xla($data, $where, $tbname);
        // var_dump ($r);die;
        if (!$list) {
            return json(['code' => 0, 'msg' => '操作失败，请重试']);
        }

        return json(['code' => 1, 'msg' => 'success', 'data' => ['list' => $list]]);
        // return json(['code' => 1, 'msg' => '操作成功']);
    }

    // 查询下拉  获取所有列表 按照类别来
    public function selDetail_xla_Bytype()
    {
        //==common
        $create_uid  = $this->admin['id'];
        $id = $this->request->param('id');
        $type = $this->request->param('type');     //下拉属性

        //

        if (trim($type) == "gsxz") {
            // 公司性质
            $tbname = "xiala_gsxz"; // gtab    

            $where = array('id' => $id);
            $data = array('update_time' => time(), 'create_uid' => $create_uid);
        } elseif (trim($type) == "gsxt") {
            // 公司形态
            $tbname = "xiala_gsxt"; // gtab  
            $where = array('id' => $id);
            $data = array('update_time' => time(), 'create_uid' => $create_uid);
        }elseif (trim($type) == "gslevel") {
            // 公司级别
            $tbname = "xiala_gslevel"; 
            $where = array('id' => $id);
            $data = array('update_time' => time(), 'create_uid' => $create_uid);
        }elseif (trim($type) == "sshy") {
            // 所属行业 
            $tbname = "xiala_sshy";   
            $where = array('id' => $id);
            $data = array('update_time' => time(), 'create_uid' => $create_uid);
        }elseif (trim($type) == "ywxz") {
            // 业务性质 
            $tbname = "xiala_ywxz"; 
            $where = array('id' => $id);
            $data = array('update_time' => time(), 'create_uid' => $create_uid);
        }
        // var

        // ----------
        $list = $this->model->selDetail_common_xla($data, $where, $tbname);
        // var_dump ($r);die;
        if (!$list) {
            return json(['code' => 0, 'msg' => '操作失败，请重试']);
        }

        return json(['code' => 1, 'msg' => 'success', 'data' => ['list' => $list[0]]]);
        // return json(['code' => 1, 'msg' => '操作成功']);
    }







    // 下拉 ed


}
